package nc.pu.pub.rest;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import nc.pub.cgdd.utils.util.JdbcUtil;
import nc.vo.pub.BusinessException;
import nccloud.api.rest.utils.ResultMessageUtil;
import nccloud.openapi.ic.m4d.MaterialOutResource;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONString;
import uap.ws.rest.util.UAPRSConstance;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Path("/receive/icOrder")
public class ReceiveIcOrderResource extends AbstractReceiveOrderResource {
    private static String HEADTABLE = "ic_material_h";
    private static String BODYTABLE = "ic_material_b";
    @POST
    @Path("add")
    @Consumes(UAPRSConstance.MEDIATYPE_JSON)
    @Produces(UAPRSConstance.MEDIATYPE_JSON)
    public Object insert(Map<String, Object>  params)  {
        JSONObject  jsonInfo=  new JSONObject(params);
        JSONArray jsonArray = jsonInfo.getJSONArray("data");
        List<Map<String, Object>> resultList = new ArrayList<>();
        this.getLogger().info("材料出库单 接收报文"+jsonInfo);
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            JSONArray bodyJson = jsonArray.getJSONObject(i).getJSONArray(BODYTABLE);
            for (int j = 0; j < bodyJson.size(); j++) {
                JSONObject jsonObject1 = bodyJson.getJSONObject(j);
                //获取物料编码
                String cmaterialCode = jsonObject1.getString("cmaterialvid");
                String cmaterialvid = null;
                try {
                    cmaterialvid = JdbcUtil.queryColumn("BD_MATERIAL", "PK_MATERIAL", "code", cmaterialCode);
                } catch (BusinessException e) {
                    throw new RuntimeException(e);
                }
                if(StringUtils.isBlank(cmaterialvid)){
                   return ResultMessageUtil.exceptionToJSON(new BusinessException("表体行传入的物料不存在请排查物料信息:"+cmaterialCode));
                }
                jsonObject1.put("cmaterialvid",cmaterialvid);
            }
            Map<String, Object> map = new HashMap<>();
            // 遍历JSONObject的键值对，添加到Map中
            java.util.Iterator<String> keys = jsonObject.keySet().iterator();
            while (keys.hasNext()) {
                String key = keys.next();
                Object value = jsonObject.get(key);
                map.put(key, value);
            }
            resultList.add(map);
        }
        MaterialOutResource  ar=new MaterialOutResource();
        JSONString save = ar.save(resultList);
        JSONObject saveResult = JSONObject.parseObject(save.toJSONString());
        Boolean success = saveResult.getBoolean("success");
        if(success){
            String [] hids=  saveResult.getJSONArray("data").stream().map((item)->{
                JSONObject headVO = ((JSONObject) item).getJSONObject("ic.MaterialOutHeadVO");
                return headVO.getString("cgeneralhid");
            }).toArray( String[]::new);
            this.getLogger().info("保存完成开始签字"+hids);
            JSONString sign = ar.sign(hids);
            this.getLogger().info("签字结果"+sign);
        }
        this.getLogger().info("响应结果"+save.toJSONString());
        return save;
    }

    @Override
    public String getModule() {
        return "pu";
    }


}
